package com.sa.app.fragments;

import java.util.List;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.sa.app.activities.ItemActivity;
import com.sa.app.objects.DatasheetLine;


public class DatasheetFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		ItemActivity act = (ItemActivity) getActivity();
		List<DatasheetLine> datasheet = act.getItemActual().getDatasheet();

		for(DatasheetLine d:datasheet){
			String k = d.getKey();
			k = k.replaceAll("-", " ");
			String firstChar = new String(k.charAt(0)+"").toUpperCase();
			String allCharsButFirst = k.substring(1);
			d.setKey(firstChar+allCharsButFirst);
		}

		LinearLayout layout = new LinearLayout(getActivity()
				.getApplicationContext());
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT));

		TextView title = new TextView(getActivity().getApplicationContext());
		title.setText(act.getItemActual().getManufacturer() + " "
				+ act.getItemActual().getModel());
		title.setTextColor(Color.WHITE);
		title.setTextSize(20);
		title.setTypeface(null, Typeface.BOLD);
		title.setPadding(10, 60, 0, 60);
		title.setGravity(Gravity.CENTER);

		layout.addView(title);

		for (int i = 0; i < datasheet.size(); i++) {

			DatasheetLine d = datasheet.get(i);

			LinearLayout line = new LinearLayout(getActivity()
					.getApplicationContext());
			line.setOrientation(LinearLayout.HORIZONTAL);
			line.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT));

			LinearLayout keyLine = new LinearLayout(getActivity()
					.getApplicationContext());
			keyLine.setOrientation(LinearLayout.HORIZONTAL);
			LinearLayout.LayoutParams keyParams = new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
					Gravity.CENTER_VERTICAL);
			// keyParams.weight = 0.5f;
			keyLine.setLayoutParams(keyParams);

			LinearLayout valueLine = new LinearLayout(getActivity()
					.getApplicationContext());
			LinearLayout.LayoutParams valueParams = new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
					Gravity.RIGHT | Gravity.CENTER_VERTICAL);
			valueParams.weight = 0.5f;
			valueLine.setOrientation(LinearLayout.HORIZONTAL);
			valueLine.setLayoutParams(valueParams);

			line.addView(keyLine);
			line.addView(valueLine);

			layout.addView(line);

			TextView key = new TextView(getActivity().getApplicationContext());
			TextView value = new TextView(getActivity().getApplicationContext());

			key.setText(d.getKey() + ": ");
			key.setPadding(30, 10, 0, 10);
			key.setTypeface(null, Typeface.BOLD);
			key.setTextSize(18);

			value.setText(d.getValue());
			value.setPadding(0, 10, 30, 10);
			value.setTextColor(Color.WHITE);
			value.setTextSize(20);

			keyLine.addView(key);
			valueLine.addView(value);

		}

		View v= (View) layout;
		if(((ViewGroup) v.getParent())!=null){
			((ViewGroup) v.getParent()).removeView(v);
		}
		return v;
	}
	//Esto hace que no cuelgue la aplicación al cambiar de activity utilizando la libreria de compatibilidad
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		setUserVisibleHint(true);
	}
}